<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="small"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="small"
          :disabled="single"
          @click="show(1)"
          v-hasPermi="['output:proShippedCheck:check']"
        >
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="small"
          :disabled="single"
          @click="show(2)"
          v-hasPermi="['output:proShippedCheck:back']"
        >
          撤回
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport(1)"
          v-hasPermi="['output:proShippedStart:export']"
        >
          EXCEL
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport(2)"
          v-hasPermi="['output:proShippedStart:export']"
        >
          出货明细
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport(3)"
          v-hasPermi="['output:proShippedStart:export']"
        >
          出货统计
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#bb0331">通过</Tag>
      <Tag type="dot" size="default" color="#0323af">检验</Tag>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      :cell-style="iCellStyle"
      stripe
      v-loading="loading"
      :data="chmMstrList"
      :height="tableH"
      @row-click="clickChm"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" width="40" type="index" align="center" />
      <el-table-column
        label="id"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmId"
      />
      <el-table-column
        label="出货单号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="chmDjbh"
      />
      <el-table-column
        label="出货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="chmChrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.chmChrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="出货类型"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmType"
      >
        <template #default="scope">
          <dict-tag :options="allOutTypeOptions" :value="scope.row.chmType" />
        </template>
      </el-table-column>
      <el-table-column
        label="客户"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmKhdm"
      >
        <template #default="scope">
          <dict-tag :options="allCustomerOptions" :value="scope.row.chmKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="出货仓库"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.chmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmNgdesc"
      />
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="chmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.chmJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="chmShrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.chmShrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="chmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="chmGzrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.chmGzrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="出货明细" name="first">
        <el-table
          border
          stripe
          :height="tableHChild"
          :data="chdDetList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="客户编号"
            align="center"
            prop="itmCustwldm"
            fixed="left"
          />
          <el-table-column label="超发单号" align="center" prop="chdCfdh" />
          <el-table-column label="销售单号" align="center" prop="chdSodh" />
          <el-table-column label="物料代码" align="center" prop="chdWldm" />
          <el-table-column label="规格描述" align="center" prop="itmPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="存放位置"
            :show-overflow-tooltip="true"
            align="center"
            prop="chdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.chdCwdm" />
            </template>
          </el-table-column>
          <el-table-column label="出库数量" align="center" prop="chdCksl" />
          <el-table-column label="出库人员" align="center" prop="chdCkry" />
          <el-table-column label="出库日期" align="center" prop="chdCkrq" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="出货统计" name="second">
        <el-table border stripe :height="tableHChild" :data="chdDetTotalList">
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="客户编号"
            align="center"
            prop="itmCustwldm"
            fixed="left"
          />
          <el-table-column label="物料代码" align="center" prop="chdWldm" />
          <el-table-column label="规格描述" align="center" prop="itmPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column label="出库数量" align="center" prop="chdCksl" />
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="daterangeChmJlrq"
              :picker-options="pickerOptions"
              size="small"
              style="width: 280px"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="出货单号" prop="chmDjbh">
            <el-input
              v-model="queryParams.chmDjbh"
              style="width: 280px"
              placeholder="请输入出货单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="出货仓库" prop="chmCkdm">
            <el-select
              v-model="queryParams.chmCkdm"
              placeholder="请选择出货仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <RadioGroup v-model="queryParams.chmZtbz" style="padding-left: 20%">
            <Radio label="3">未审核</Radio>
            <Radio label="4"><span style="color: blue">已审核</span></Radio>
            <Radio label="5">全部</Radio>
          </RadioGroup>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 撤回 -->
    <Modal v-model="check_back" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认撤回重写？</span>
      </p>
      <div style="text-align: center">
        <p>撤回理由</p>
        <Input v-model="back_reason" style="width: 75%" />
      </div>
      <div slot="footer">
        <Button type="error" size="large" long @click="handleBack">撤回</Button>
      </div>
    </Modal>
    <!-- 审核 -->
    <Modal v-model="check_check" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>确认</span>
      </p>
      <div style="text-align: center">
        <p>确认将【入库单号：{{ chmMstrs.chmDjbh }}】的材料进行审核作业吗？</p>
        <p>一旦操作无法更改，材料将进入仓库过账作业，请谨慎操作。</p>
      </div>
      <div slot="footer">
        <Button type="success" size="large" long @click="handleCheck"
          >审核</Button
        >
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  listChmMstr,
  listChdDet,
  chmMstrBack,
  chmMstrCheck,
  exportProShipped,
} from "@/api/output/proShipped";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { getAllCustomerTwo } from "@/api/project/customer";

export default {
  name: "proShippedCheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      //表格高度
      tableH: 250,
      tableHChild: 300,
      tableHAdd: document.body.clientHeight - 320,
      activeTab: "first",
      // 有效和所有的出货类型字典
      allOutTypeOptions: [],
      //单位字典
      unitFlagOptions: [],
      //默认储位字典
      allCwOptions: [],
      //所有仓库
      allStorageOptions: [],
      storageOptions: [],
      //所有客户类型
      allCustomerOptions: [],
      // 选中id
      ids: null,
      chmMstrs: {},
      //审核、撤回
      check_check: false,
      check_back: false,
      back_reason: null,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 产品出货表格数据
      chmMstrList: [],
      // 产品出货详细表格数据
      chdDetList: [],
      // 产品出货统计表格数据
      chdDetTotalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 开单日期时间范围
      daterangeChmJlrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        chmDjbh: null,
        chmZtbz: "3",
        chmJlrq: null,
        chmCkdm: "",
        index: 0,
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    //所有出货类型
    this.getAllDicts("sys_out_type").then((response) => {
      this.allOutTypeOptions = response.data;
    });
    //所有仓库
    getAll().then((response) => {
      this.allStorageOptions = response.data;
      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //有效仓库
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;
      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //所有客户
    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }
      this.allCustomerOptions = response.data;
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //存放位置
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
  },
  methods: {
    /** 颜色事件 */
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.chmZtbz == "1" && row.chmTjflag == "1") {
        return "color:#0fac09";
      }
      //通过
      else if (row.chmZtbz == "2" && row.chmTjflag == "1") {
        return "color:#0323af";
      }
      //检验
      else if (row.chmZtbz == "6" && row.chmTjflag == "1") {
        return "color:#bb0331";
      }
    },
    /** 查询产品出货列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeChmJlrq && "" != this.daterangeChmJlrq) {
        this.queryParams.params["beginChmJlrq"] = this.moment(
          this.daterangeChmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endChmJlrq"] = this.moment(
          this.daterangeChmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listChmMstr(this.queryParams).then((response) => {
        this.chmMstrList = [];
        this.chmMstrList = this.chmMstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickChm(row);
        } else if (this.chmMstrList != null && this.chmMstrList.length > 0) {
          this.clickChm(this.chmMstrList[0]);
        }
      });
    },
    getChdDetList(chdDjbh) {
      listChdDet(chdDjbh).then((response) => {
        this.chdDetTotalList = response.data.total;
        this.chdDetList = response.data.detail;
      });
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listChmMstr(this.queryParams).then((response) => {
          this.chmMstrList = this.chmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.chmMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    //单击选中数据
    clickChm(row) {
      if (row != null) {
        this.single = row == null || row.chmZtbz != "1" || row.chmTjflag != "1";
        this.ids = row.chmId;
        this.chmMstrs = row;
        this.getChdDetList(row.chmDjbh);
      }
    },
    /** 审核、撤回操作 */
    show(e) {
      if (this.chmMstrs.chmZtbz != 1 || this.chmMstrs.chmTjflag != 1) {
        this.$Modal.warning({
          title: "提示",
          content: "该单据已审核，不允许此操作",
        });
      } else {
        if (e == 1) {
          this.check_check = true;
        } else {
          this.check_back = true;
        }
      }
    },
    /** 审核 */
    handleCheck() {
      chmMstrCheck(this.chmMstrs.chmDjbh)
        .then((res) => {
          this.msgSuccess("审核单据成功~");
          this.check_check = false;
          this.queryParams.pageNum = 1;
          this.chmMstrList = [];
          this.getList();
        })
        .catch(() => {
          this.msgError("审核单据失败~");
        });
    },
    /** 撤回 */
    handleBack() {
      chmMstrBack(this.chmMstrs.chmId, this.back_reason)
        .then((res) => {
          this.msgSuccess("撤回成功~");
          this.check_back = false;
          this.queryParams.pageNum = 1;
          this.chmMstrList = [];
          this.getList();
        })
        .catch(() => {
          this.msgError("撤回失败~");
        });
    },
    handleSearch() {
      this.title = "查询产品出货";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport(index) {
      this.queryParams.index = index;
      this.queryParams.chmDjbh = this.chmMstrs.chmDjbh;
      if (index == 2 || index == 3) {
        if (
          this.queryParams.chmDjbh == "" ||
          this.queryParams.chmDjbh == null
        ) {
          this.$Modal.warning({
            title: "提示",
            content: "请选择需要导出的单据",
          });
        }
      }
      const queryParams = this.queryParams;
      exportProShipped(queryParams)
        .then((res) => {
          this.download(res.msg);
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
